package com.ftg.learn.day11;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 学习一下反射里面的方法
 *
 * @author 布衣
 */
public class LearnMethod02 {

    public static void main(String[] args) {
        //class method field
        Class<Shop> shop1 = Shop.class;

        //获取所有方法
        Method[] declaredMethods = shop1.getDeclaredMethods();

        for (Method declaredMethod : declaredMethods) {
            System.out.println("获取method类对应的方法里面的全部参数类型");
            System.out.print(declaredMethod.getName()+"==>");
            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            System.out.println(Arrays.toString(parameterTypes));

        }

        try {
            Method readMethod = shop1.getDeclaredMethod("getName");
            Method writeMethod = shop1.getDeclaredMethod("setName", String.class);

            Shop shop = shop1.newInstance();

            //调用一下setter
            System.out.println("获取method类对应的方法里面的全部参数类型");
            Class<?>[] parameterTypes = writeMethod.getParameterTypes();
            System.out.println(Arrays.toString(parameterTypes));

            System.out.println(writeMethod.getReturnType()+"<==set方法返回值类型");

            Object obj1 = writeMethod.invoke(shop,"超市");
            System.out.println(obj1+"我是反射调用的setname方法");
            //调用一下get方法
            System.out.println(readMethod.getReturnType()+"<==get方法返回值类型");
            Object obj = readMethod.invoke(shop);
            System.out.println(obj+"我是调用的getname方法");


            System.out.println("单个方法==>" + shop1.getDeclaredMethod("getName").getName());
        } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException|InstantiationException e) {
            e.printStackTrace();
        }


    }

}
